package pers.zc.activiti.thread.demo;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import pers.zc.activiti.thread.closethread.CloseThread;

import java.util.Timer;
import java.util.concurrent.Future;

/**
 * @description 线程池测试2
 * @Author Lin FuYuan
 * @date 2020/4/16
 */

@Service
public class AsyncDemoTwo extends Timer {
    @Async("testThreadPool")
    public Future<Integer> testTwo()  {
        Thread thread = Thread.currentThread();
        CloseThread.list.add(thread);
        try {
            for (int i = 0; i <10 ; i++) {
                if(Thread.interrupted()){
                    System.out.println("线程终止");
                    return new AsyncResult<Integer>(1);
                }
//                Thread.sleep(1000);
                System.out.println("testTwo: "+thread.getName()+" -> "+ i);
            }
        } catch (Exception e){
            System.out.println("停止线程");
            return new AsyncResult<Integer>(1);
        }
        return new AsyncResult<Integer>(1);
    }
}
